package com.relax.mylock;

import com.relax.reentrantLock.ReadWrireLock;

import javax.lang.model.element.VariableElement;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class Demo {
    private int i;

    Lock lock = new ReentrantLock();




    public void a(Thread thread2,boolean interfa) {
        if(interfa){
            thread2.interrupt();
            System.out.println(thread2.getName()+"被中断");
        }

        lock.lock();
        boolean interrupted = Thread.currentThread().isInterrupted();
        System.out.println(Thread.currentThread().getName()+"---是否被中断"+interrupted);

        if(!interrupted){
            try {
                Thread.sleep(20000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

        System.out.println(Thread.currentThread().getName()+"---执行结束"+interrupted);

        lock.unlock();
    }





    public static void main(String[] args) throws InterruptedException {
        AtomicInteger atomicInteger = new AtomicInteger();
        atomicInteger.getAndIncrement();
        final Demo d = new Demo();

        new Thread(new Runnable() {
            public void run() {
                d.a(null,false);
            }
        }).start();

        Thread.sleep(5000);

        Thread thread2 = new Thread(new Runnable() {
            public void run() {
                d.a(null,false);
            }
        });

        thread2.start();


        Thread.sleep(5000);

        new Thread(new Runnable() {
            public void run() {
                d.a(thread2,true);
            }
        }).start();



        System.out.println("主线程先抢到资源。。。。。");

    }
}
